<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/9/6
 * Time: 21:57
 */
namespace merchant\models;

use Yii;
use yii\helpers\ArrayHelper;

class BaseModel extends \yii\db\ActiveRecord
{

    public static function findOne($condition)
    {
        if (!ArrayHelper::isAssociative($condition)) {
            // query by primary key
            $primaryKey = static::primaryKey();
            if (isset($primaryKey[0])) {
                // if condition is scalar, search for a single primary key, if it is array, search for multiple primary key values
                $condition = [$primaryKey[0] => is_array($condition) ? array_values($condition) : $condition];
            } else {
                throw new InvalidConfigException('"' . get_called_class() . '" 找不到记录.');
            }
        }
        if (Yii::$app->user->can('Admin') == false) {
            $condition['store_id'] = Yii::$app->store->id;
        }

        return parent::findOne($condition); // TODO: Change the autogenerated stub
    }

    public function beforeSave($insert)
    {
        if (Yii::$app->user->can('Admin') == false) {
            $this->store_id = Yii::$app->store->id;
        }

        return parent::beforeSave($insert); // TODO: Change the autogenerated stub
    }
}